<template>
  <div class="register-container">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        autocomplete
        :rules="[{ required: true, message: '请填写用户名' }]"
      />

      <van-field
        v-model="password"
        type="password"
        name="密码"
        autocomplete
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />

      <van-field
        v-model="repassword"
        type="password"
        name="确认密码"
        autocomplete
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">注册</van-button>
      </div>
    </van-form>

    <div class="text">
      <router-link to="/login">有账号,我要登陆</router-link>
    </div>
  </div>
</template>

<script>
import { reqRegister } from '@/api/user'
export default {
  name: 'register',
  data() {
    return {
      username: '',
      password: '',
      repassword: '',
    }
  },
  methods: {
    async onSubmit() {
      if (this.password !== this.repassword) {
        this.$toast('两次密码不一致')
        return
      }
      let data = { username: this.username, password: this.password }
      let { message, status } = await reqRegister(data)
      this.$toast(message)
      if (status === 0) {
        this.$router.push('/login')
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.register-container {
  padding-top: 20px;

  .text {
    text-align: center;

    a {
      color: rgb(185 181 181);
    }
  }
}
</style>
